# app/routes/grades.py

from flask import Blueprint, render_template, request, redirect, url_for, flash
from app.models.models import Submissions, Assignments
from app import db

grades = Blueprint('grades', __name__)

@grades.route('/grades')
def list_grades():
    # 获取所有提交并计算总成绩
    submissions = Submissions.query.all()
    total_grades = {}
    for submission in submissions:
        if submission.student_id not in total_grades:
            total_grades[submission.student_id] = 0
        total_grades[submission.student_id] += submission.grade or 0
    return render_template('grades/list.html', total_grades=total_grades)

@grades.route('/submission/grade/<int:id>', methods=['GET', 'POST'])
def grade_submission(id):
    submission = Submissions.query.get_or_404(id)
    if request.method == 'POST':
        submission.grade = request.form.get('grade')
        db.session.commit()
        flash('Submission graded successfully')
        return redirect(url_for('grades.list_grades'))
    return render_template('grades/grade.html', submission=submission)
